console.log(edger)
console.log(io)

const socketClient = io()

let devid_wether;
let devid_model_control;
let devid_tempre_control;
let devid_gas;


socketClient.on('connect', () => {
  console.log('socket client connect')

  setTimeout(() => {
    socketClient.emit('init', (data) => {
      console.log('[init]: ', data)
    })
  
    
    socketClient.emit('device-list', (data) => {
      console.log('[device-list]: ', data)

      
      for(let i=0; i<data.length; i++){
        let item = data[i];
        console.log(item);

        if(item.alias=="天气传感器"){
          devid_wether = item.devid;     
          socketClient.emit('select-device', devid_wether, (res) => {
            console.log('[select-device]: ', res)
          })        
        }else if(item.alias=="空调模式遥控"){
          devid_model_control = item.devid;
          socketClient.emit('select-device', devid_model_control, (res) => {
            console.log('[select-device]: ', res)
          })
        }else if(item.alias=="燃气传感器"){
          devid_gas = item.devid;
          socketClient.emit('select-device', item.devid, (res) => {
            console.log('[select-device]: ', res)
          })
        }else if(item.alias=="温度控制器"){
          devid_tempre_control = item.devid;
          socketClient.emit('select-device', item.devid, (res) => {
            console.log('[select-device]: ', res)
          })
        }
      }
    })
    
  }, 1000)
})

//定时更新后端的devid
setInterval(() => {
  socketClient.emit("submit_devid", {
    devid_model_control:devid_model_control,
    devid_wether: devid_wether,
    devid_tempre_control: devid_tempre_control
  }, (data)=>{
    // console.log(data);
  })  
}, 1500);


function formatFloat(num, n = 1, flag) {
  var f = parseFloat(num);
  if (isNaN(f)) {
      return false;
  }
  if (flag === 'ceil') {  //向上取整
      f = Math.ceil(num * Math.pow(10, n)) / Math.pow(10, n); // n 幂
  } else if (flag === 'floor') {  //向下取整
      f = Math.floor(num * Math.pow(10, n)) / Math.pow(10, n); // n 幂
  } else {  //四舍五入
      f = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); // n 幂
  }
  var s = f.toString();
  var rs = s.indexOf('.');
  //判定如果是整数，增加小数点再补0
  if (rs < 0) {
      rs = s.length;
      s += '.';
  }
  while (s.length <= rs + n) {
      s += '0';
  }
  return s;
}


//检测天气传感器
let interval_people = true; //是否有人， 默认为有人
let inner_tempre = 25;      //室内温度
socketClient.on("weather", (data)=>{
  console.log(data);

  let t = document.getElementById("inner-tempre");
  let h = document.getElementById("inner-hum");

  if(data.method=="report-environment"){
    let tempreture = formatFloat(data.tempre, 1, "other");
    let hum = formatFloat(data.hum, 1, "other");

    inner_tempre = tempreture;
    t.innerText = tempreture + "℃";
    h.innerText = hum + "%";
  }else if(data.method=="report-people"){
    if(data.people=="true"){
      interval_people = true;
    }else{
      interval_people = false;
    }
  
    socketClient.emit("update_var", {interval_people: interval_people}, (data)=>{
      console.log(data);
    });
  }
})

socketClient.on('error', (msg) => {
  // let alert_sheet = document.getElementById("alert");
  // alert_sheet.innerText = msg;
  // alert_sheet.style.transform = "translateY(0)";
  // setTimeout(() => {
  //   alert_sheet.style.transform = "translateY(-100%)";
  // }, 3000);
})

socketClient.on('lost', (dev) => {
  
  let alert_sheet = document.getElementById("alert");
  if(alert_sheet.style.transform == "translateY(0)"){
    alert_sheet.style.transform = "translateY(-100%)";
    setTimeout(() => {
      alert_sheet.innerText = '设备' + dev.alias + '上线！';
      alert_sheet.style.transform = "translateY(0)";
      setTimeout(() => {
        alert_sheet.style.transform = "translateY(-100%)";
      }, 3000);
    }, 400);
  }else{
    alert_sheet.innerText = '设备' + dev.alias + '上线！';
    alert_sheet.style.transform = "translateY(0)";
    setTimeout(() => {
      alert_sheet.style.transform = "translateY(-100%)";
    }, 3000);
  }
})

socketClient.on('join', (dev) => {
  let alert_sheet = document.getElementById("alert");
  if(alert_sheet.style.transform == "translateY(0)"){
    alert_sheet.style.transform = "translateY(-100%)";
    setTimeout(() => {
      alert_sheet.innerText = '设备' + dev.alias + '上线！';
      alert_sheet.style.transform = "translateY(0)";
      setTimeout(() => {
        alert_sheet.style.transform = "translateY(-100%)";
      }, 3000);
    }, 400);
  }else{
    alert_sheet.innerText = '设备' + dev.alias + '上线！';
    alert_sheet.style.transform = "translateY(0)";
    setTimeout(() => {
      alert_sheet.style.transform = "translateY(-100%)";
    }, 3000);
  }
  if(dev.alias=="天气传感器"){
    devid_wether = dev.devid;
  }else if(dev.alias=="空调模式遥控"){
    devid_model_control = dev.devid;
  }else if(dev.alias=="燃气传感器"){
    devid_gas = dev.devid;
  }else if(dev.alias=="温度控制器"){
    devid_tempre_control = dev.devid;
  }

  // console.log(devid_tempre_control, dev.devid); 
  socketClient.emit('select-device', dev.devid, (res) => {
    console.log('[select-device]: ', res)
  })

})

socketClient.on('device-list', (list) => {
  console.log('[device-list]:', list)
})



























/*
以下为onclick函数
*/ 



let Switch = "open";        //默认初始为开启空调指令
//模式初始化
let Model = {}
Model.now = 0;
Model.auto = 0;
Model.cold = 1;
Model.hot = 2;
Model.hum = 3;
Model.wind = 4;

let switch_speed = 1;       //风速档位，默认为1
let switch_power = false;   //强劲档位 默认为关闭
let switch_sweep = false;   //扫风档位 默认为手动
let switch_light = true;    //灯光模式 默认为开

let settingTempre = 25;     //设定的温度
let restTime = 0;           //定时剩余分钟数
                            //后期考虑将其挂载到后端实现
let interval_Timeset;       //定时的interval  存放interval函数的句柄

let interval_off = 30;      //无人多少分钟后关闭空调
let autoClose = false;      //无人自动关机开关
let openClose = false;      //开机自动关窗
let position = 0;           //初始位置  0表示完全关闭

let temp_gate = false;      //是否启用温度阈值  默认不启用
let max_tempre = 30;        //上限温度
let min_tempre = 10;        //下限温度
let alert_level = 0;        //警报等级
let setting_alert = 3;      //设定警报等级

socketClient.on("load", (data)=>{
  if(data.Switch){
    Switch = data.Switch;
    let icon = document.getElementById("switchicon");   //图标更改
    if(data.Switch=="open"){
      icon.style.backgroundImage = "url(./img/switch/on.png)";
    }else{
      icon.style.backgroundImage = "url(./img/switch/off.png)";
    }
  }
  if(data.Model!=Model.now){
    Model.now = data.Model;

    let icon = document.getElementById("modelid");
    let icon_text = document.getElementById("modeltext");
    if(data.Model==Model.auto){
      icon.style.backgroundImage = "url(./img/model/auto.png)";
      icon_text.innerText = "自动";
    }else if(data.Model==Model.cold){
      icon.style.backgroundImage = "url(./img/model/cold.png)";
      icon_text.innerText = "制冷";
    }else if(data.Model==Model.hot){
      icon.style.backgroundImage = "url(./img/model/hot.png)";
      icon_text.innerText = "制热";
    }else if(data.Model==Model.hum){
      icon.style.backgroundImage = "url(./img/model/hum.png)";
      icon_text.innerText = "除湿";
    }else if(data.Model==Model.wind){
      icon.style.backgroundImage = "url(./img/model/wind.png)";
      icon_text.innerText = "送风";
    }
  }
  if(data.switch_speed!=switch_speed){
    switch_speed = data.switch_speed;

    let icon = document.getElementById("model-speed");
    let icon_text = document.getElementById("text-speed");

    icon.style.backgroundImage = "url(./img/speed/" + switch_speed +".png)";
    switch(switch_speed){
      case 1: icon_text.innerText = "风速 低";
              break;
      case 2: icon_text.innerText = "风速 中";
              break;
      case 3: icon_text.innerText = "风速 高";
              break;
    }
  }
  if(data.switch_light!=switch_light){
    switch_light = data.switch_light;
  }
  if(data){     //switch_power
    switch_power = data.switch_power;
    if(switch_power){
      let icon = document.getElementById("model-speed");
      let icon_text = document.getElementById("text-speed");
      icon.style.backgroundImage = "url(./img/speed/4.png)";
      icon_text.innerText = "强劲"
    }
  }
  if(switch_sweep!=data.switch_sweep){
    switch_sweep = data.switch_sweep;
    let icon = document.getElementById("model-windtype");
    let icon_text = document.getElementById("text-windtype");
    if(switch_sweep){
      icon.style.backgroundImage = "url(./img/speed/auto.png)";
      icon_text.innerText = "自动扫风";
    }else{
      icon.style.backgroundImage = "url(./img/speed/hand.png)";
      icon_text.innerText = "手动扫风";
    }
  }
  if(data.settingTempre!=settingTempre){
    settingTempre = data.settingTempre;
    let tempre_text = document.getElementById("tempre_text");
    tempre_text.innerText = settingTempre;
  }
  if(restTime!=data.restTime){
    restTime = data.restTime;

    let view = document.getElementById("restTime");
    let val_min = restTime % 60;
    let val_hour = (restTime-val_min)/60;
    if( restTime!=0){
      view.innerText = val_hour + ":" + val_min;
    }else{
      view.innerText = "--:--";
    }
  }

  interval_off = data.interval_off;
  document.getElementById("input-setting-interval").placeholder = interval_off;
  position = data.position;
  document.getElementById("windowRange").value = position;

  autoClose = data.autoClose; //离开自动关闭
  if(autoClose){
    document.getElementById("OnautoClose").checked = true;
    document.getElementById("OffautoClose").checked = false;
    document.getElementById("inputInterval").style.display = "flex";
  }

  openClose = data.openClose; //开机自动开窗
  if(openClose){
    document.getElementById("OnopenClose").checked = true;
    document.getElementById("OffopenClose").checked = false;
  }
  
  temp_gate = data.temp_gate; 
  if(temp_gate){
    document.getElementById("ongate").checked = true;
    document.getElementById("offgate").checked = false;
    document.getElementById("inputTempre").style.display = "flex";
    document.getElementById("inputLowTempre").style.display = "flex";
  }

  max_tempre = data.max_tempre;
  document.getElementById("input-high-tempre").placeholder = max_tempre;
  
  min_tempre = data.min_tempre;
  document.getElementById("input-low-tempre").placeholder = min_tempre;

  setting_alert = data.setting_alert_level;
  let alert_radio;
  let i;
  for(i=1; i<=5; i++){
    alert_radio = document.getElementById("alert_"+i);
    if(i==setting_alert){
      alert_radio.checked = true;
      break;
    }else{
      alert_radio.checked = false;
    }
  }


  // console.log(data);
  })

//开关按键
function openSwitch(event){
    let now = document.getElementById("openswitch");
    now.style.backgroundColor = "#ccc";
    setTimeout(()=>{
        now.style.backgroundColor = "white";                //颜色变化
        let icon = document.getElementById("switchicon");   //图标更改

        if(Switch=="open"){
            icon.style.backgroundImage = "url(./img/switch/off.png)";
            Switch = "close";

            //更新后端变量
            socketClient.emit("update_var", {Switch: "close"}, (data)=>{
              console.log(data);
            })
            //向后端发送开启报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "1",
                channel: "on"
              }
            }, (data)=>{
              console.log(data);
            })
            //若开机自动关窗
            if(openClose){
              socketClient.emit("model_change", devid_model_control, 
              {
                method: "window",
                position: "100"
              }, (data)=>{
                console.log(data);
              })
            
              document.getElementById("windowRange").value = 100;
            }          
        }else if(Switch=="close"){
            icon.style.backgroundImage = "url(./img/switch/on.png)";
            Switch = "open";
            //更新后端变量
            socketClient.emit("update_var", {Switch: "open"}, (data)=>{
              console.log(data);
            })
            //向后端发送关闭报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "1",
                channel: "off"
              }
            }, (data)=>{
              console.log(data);
            })
        }
    }, 100);

    
}

//模式按键
function openModel(event){
    let now = document.getElementById("openmodel");
    now.style.backgroundColor = "#ccc";
    setTimeout(() => {
        now.style.backgroundColor = "white";        //更改背景色
        //切换模式图标 文字等
        let icon = document.getElementById("modelid");
        let icon_text = document.getElementById("modeltext");
        if(Model.now == Model.auto){
            icon.style.backgroundImage = "url(./img/model/cold.png)";
            icon_text.innerText = "制冷";
            Model.now = Model.cold;
            //发送更换到制冷的报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "3",
                model: "cold"
              }
            }, (data)=>{
              console.log(data);
            })

        }else if(Model.now == Model.cold){
            icon.style.backgroundImage = "url(./img/model/hot.png)";
            icon_text.innerText = "制热";
            Model.now = Model.hot;
            //发送更换到制热的报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "3",
                model: "hot"
              }
            }, (data)=>{
              console.log(data);
            })

        }else if(Model.now == Model.hot){
            icon.style.backgroundImage = "url(./img/model/hum.png)";
            icon_text.innerText = "除湿";
            Model.now = Model.hum;
            //发送更换到除湿的报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "3",
                model: "hum"
              }
            }, (data)=>{
              console.log(data);
            })

        }else if(Model.now == Model.hum){
            icon.style.backgroundImage = "url(./img/model/wind.png)";
            icon_text.innerText = "送风";
            Model.now = Model.wind;
            //发送更换到送风的报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "3",
                model: "wind"
              }
            }, (data)=>{
              console.log(data);
            })

        }else if(Model.now == Model.wind){
            icon.style.backgroundImage = "url(./img/model/auto.png)";
            icon_text.innerText = "自动";
            Model.now = Model.auto;
            //发送更换到自动的报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "3",
                model: "auto"
              }
            }, (data)=>{
              console.log(data);
            })
        }

        socketClient.emit("update_var", {Model: Model.now}, (data)=>{
          console.log(data);
        })
    }, 100);
}


//风速按键
function openWindSpeed(event){
    let now = document.getElementById("speed");
    now.style.backgroundColor = "#ccc";

    let icon = document.getElementById("model-speed");
    let icon_text = document.getElementById("text-speed");
    setTimeout(() => {
        now.style.backgroundColor = "white";
        switch_power = false;

        if(switch_speed == 1){
            icon.style.backgroundImage = "url(./img/speed/2.png)"
            icon_text.innerText = "风速 中"
            switch_speed = 2;
            //发送更改风速为中的报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "4",
                speed: "2"
              }
            }, (data)=>{
              console.log(data);
            })
        }else if(switch_speed == 2){
            icon.style.backgroundImage = "url(./img/speed/3.png)"
            icon_text.innerText = "风速 高"
            switch_speed = 3;
            //发送更改风速为高的报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "4",
                speed: "3"
              }
            }, (data)=>{
              console.log(data);
            })
        }else if(switch_speed == 3){
            icon.style.backgroundImage = "url(./img/speed/1.png)"
            icon_text.innerText = "风速 低"
            switch_speed = 1;
            //发送更改风速为低的报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "4",
                speed: "1"
              }
            }, (data)=>{
              console.log(data);
            })
        }

        //更新后端变量
        socketClient.emit("update_var", {switch_speed: switch_speed}, (data)=>{
          console.log(data);
        })
    }, 100);
}

//强劲按键
function openPower(event){
    let now = document.getElementById("power");
    now.style.backgroundColor = "#ccc";

    let icon = document.getElementById("model-speed");
    let icon_text = document.getElementById("text-speed");
    setTimeout(() => {
        now.style.backgroundColor = "white";

        if(switch_power==false){
            icon.style.backgroundImage = "url(./img/speed/4.png)"
            icon_text.innerText = "强劲";
            switch_power = true;
            //发送改变风力为强劲的报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "4",
                speed: "4"
              }
            }, (data)=>{
              console.log(data);
            })
        }else if(switch_power==true){
            icon.style.backgroundImage = "url(./img/speed/" + switch_speed + ".png)";
            let add = "";
            switch(switch_speed){
                case 1: add = "低";
                        break;
                case 2: add = "中";
                        break;
                case 3: add = "高";
                        break;
            }
            icon_text.innerText = "风速 "+add;
            switch_power = false;
            //根据switch_speed不同 发送改变模式的报文
            switch(switch_speed){
              case 1: socketClient.emit("model_change", devid_model_control, {
                        method: "set",
                        data: {
                          type: "4",
                          speed: "1"
                        }
                      }, (data)=>{
                        console.log(data);
                      })
                      break;
              case 2: socketClient.emit("model_change", devid_model_control, {
                        method: "set",
                        data: {
                          type: "4",
                          speed: "2"
                        }
                      }, (data)=>{
                        console.log(data);
                      })
                      break;
              case 3: socketClient.emit("model_change", devid_model_control, {
                        method: "set",
                        data: {
                          type: "4",
                          speed: "3"
                        }
                      }, (data)=>{
                        console.log(data);
                      })
                      break;
            }
        }
        // 更新后端变量
        socketClient.emit("update_var", {switch_power: switch_power}, (data)=>{
          console.log(data);
        })
    }, 100);

}


//扫风按键
function openSweep(event){
    let now = document.getElementById("sweep");
    now.style.backgroundColor = "#ccc";

    let icon = document.getElementById("model-windtype");
    let icon_text = document.getElementById("text-windtype");
    setTimeout(() => {
        now.style.backgroundColor = "white";

        if(switch_sweep == false){
            icon.style.backgroundImage = "url(./img/speed/auto_wind.png)";
            icon_text.innerText = "自动扫风";
            switch_sweep = true;
            //发送自动扫风报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "5",
                sweep: "on"
              }
            }, (data)=>{
              console.log(data);
            })

            //自动扫风时将风速设置为高
            switch_speed = 3;
            let icon_speed = document.getElementById("model-speed");
            let icon_text_speed = document.getElementById("text-speed");

            icon_speed.style.backgroundImage = "url(./img/speed/3.png)";
            icon_text_speed.innerText = "风速 高"

        }else if(switch_sweep == true){
            icon.style.backgroundImage = "url(./img/speed/hand.png)";
            icon_text.innerText = "手动扫风";
            switch_sweep = false;
            //发送手动扫风报文
            socketClient.emit("model_change", devid_model_control, {
              method: "set",
              data: {
                type: "5",
                sweep: "off"
              }
            }, (data)=>{
              console.log(data);
            })
        }
        // 更新后端变量
        socketClient.emit("update_var", {switch_sweep: switch_sweep}, (data)=>{
          console.log(data);
        })
    }, 100);
}

//温度下降按键
function openDecrease(event){
    let now = document.getElementById("-");
    now.style.backgroundColor = "#ccc";

    let tempre_text = document.getElementById("tempre_text");
    let now_tempre = Number(tempre_text.innerText);
    setTimeout(() => {
        now.style.backgroundColor = "white";
        if(now_tempre<=16){
            //alert(最低温度)
        }else{
            tempre_text.innerText = now_tempre-1;
            //发送更改温度报文
            socketClient.emit("model_change", devid_tempre_control, {
              method: "set",
              data: {
                type: "2",
                tempre: --settingTempre
              }
            }, (data)=>{
              console.log(data);
            })
            //模式改变
            if(Model.now==Model.auto){
              let icon = document.getElementById("model-speed");
              let icon_text = document.getElementById("text-speed");
              icon.style.backgroundImage = "url(./img/speed/2.png)";
              icon_text.innerText = "风速 中";
              icon = document.getElementById("modelid");
              icon_text = document.getElementById("modeltext");
              icon.style.backgroundImage = "url(./img/model/cold.png)";
              icon_text.innerText = "制冷";
              Model.now = Model.cold;
              switch_speed = 2;
              socketClient.emit("update_var", {Model: Model.now, switch_speed:switch_speed},(data)=>{
                console.log(data);
              })
            }
        }
        // 更新后端变量
        socketClient.emit("update_var", {settingTempre: settingTempre}, (data)=>{
          console.log(data);
        })
    }, 100);
}

//温度上升按键
function openIncrease(event){
    let now = document.getElementById("+");
    now.style.backgroundColor = "#ccc";

    let tempre_text = document.getElementById("tempre_text");
    let now_tempre = Number(tempre_text.innerText);
    setTimeout(() => {
        now.style.backgroundColor = "white";
        if(now_tempre>=30){
            //alert(最高温度)
        }else{
            tempre_text.innerText = now_tempre+1;
            //发送更改温度报文
            socketClient.emit("model_change", devid_tempre_control, {
              method: "set",
              data: {
                type: "2",
                tempre: ++settingTempre
              }
            }, (data)=>{
              console.log(data);
            })
            //模式改变
            if(Model.now==Model.auto){
              let icon = document.getElementById("model-speed");
              let icon_text = document.getElementById("text-speed");
              icon.style.backgroundImage = "url(./img/speed/2.png)";
              icon_text.innerText = "风速 中";
              icon = document.getElementById("modelid");
              icon_text = document.getElementById("modeltext");
              icon.style.backgroundImage = "url(./img/model/cold.png)";
              icon_text.innerText = "制冷";
              Model.now = Model.cold;
              switch_speed = 2;
              socketClient.emit("update_var", {Model: Model.now, switch_speed:switch_speed},(data)=>{
                console.log(data);
              })
            }
        }
        // 更新后端变量
        socketClient.emit("update_var", {settingTempre: settingTempre}, (data)=>{
          console.log(data);
        })
    }, 100);
}

//定时按键
function openTimeSet(event){
    let now = document.getElementById("timeset");
    let page = document.getElementById("gray");
    let inputHour = document.getElementById("inputHour");
    let inputMin = document.getElementById("inputMin");
    now.style.backgroundColor = "#ccc";

    setTimeout(() => {
        now.style.backgroundColor = "white";
        // page.style.display = "flex";
        if(Switch=="close"){
        page.style.transform = "translateX(0)";
        inputHour.value = null;
        inputMin.value = null;
        }else{
          let alert_sheet = document.getElementById("alert");
          alert_sheet.innerText = "开机后才可定时关机";
          alert_sheet.display.transform = "translateY(0)";
          setTimeout(() => {
            alert_sheet.display.transform = "translateY(-100%)";
          }, 3000);
        }
    }, 100);
}

//灯光按键
function openLight(event){
    let now = document.getElementById("light");
    now.style.backgroundColor = "#ccc";
    setTimeout(()=>{
        now.style.backgroundColor = "white";

        if(switch_light){
          switch_light = false;
          //发送报文
          socketClient.emit("model_change", devid_model_control, {
            method: "set",
            data: {
              type: "6",
              light: "off"
            }
          }, (data)=>{
            console.log(data);
          })
        }else{
          switch_light = true;
          //发送报文
          socketClient.emit("model_change", devid_model_control, {
            method: "set",
            data: {
              type: "6",
              light: "on"
            }
          }, (data)=>{
            console.log(data);
          })
        }

        socketClient.emit("update_var", {switch_light: switch_power}, (data)=>{
          console.log(data);
        })
    }, 100); 
    
}


//设置按键
function openSetting(event){
    let now = document.getElementById("setting");
    let setting = document.getElementById("setting-page");
    let return_sheet = document.getElementById("return-sheet");

    now.style.backgroundColor = "#ccc";
    setTimeout(()=>{
        now.style.backgroundColor = "white";
        
        setting.style.transform = "translateX(0)";
        return_sheet.style.transform = "translateX(0)";
    }, 100); 
    
}


//温度点击设置
function onInputTempreture(event){
    let now = document.getElementById("tempre_sheet");
    settingTempre = Number(now.innerText);
    now.innerHTML = "<input autofocus id='Tempreture' placeholder="+settingTempre+" type='number' onblur='closeInputTempreture(event)'>";
}


//温度完成输入
function closeInputTempreture(event){
    let now = document.getElementById("tempre_sheet");
    let input = document.getElementById("Tempreture");
    let val = input.value;
    let setval = Number(val);
    if(val!=""){
        val = Number(val);
        if(val<16 || val>30){
            // alert("超出范围");
            setval = settingTempre;
        }else{
            settingTempre = setval;
            //发送更改温度报文
            socketClient.emit("model_change", devid_tempre_control, {
              method: "set",
              data: {
                type: "2",
                tempre: settingTempre
              }
            }, (data)=>{
              console.log(data);
            })
            //模式改变
            if(Model.now == Model.auto){
              let icon = document.getElementById("model-speed");
              let icon_text = document.getElementById("text-speed");
              icon.style.backgroundImage = "url(./img/speed/2.png)";
              icon_text.innerText = "风速 中";
              icon = document.getElementById("modelid");
              icon_text = document.getElementById("modeltext");
              icon.style.backgroundImage = "url(./img/model/cold.png)";
              icon_text.innerText = "制冷";
              Model.now = Model.cold;
              switch_speed = 2;
              socketClient.emit("update_var", {Model: Model.now, switch_speed:switch_speed},(data)=>{
                console.log(data);
              })
            }
        }
        now.innerHTML = "<div id='tempre_text' onclick=\"onInputTempreture(event)\" >"+ setval +"</div>";
        //更新后端变量
        socketClient.emit("update_var", {settingTempre: settingTempre}, (data)=>{
          console.log(data);
        })
    
      }else{
        now.innerHTML = "<div id='tempre_text' onclick=\"onInputTempreture(event)\" >"+ settingTempre +"</div>";
    }
}

//更新小时输入框
function updateHour(event){
    let now = document.getElementById("inputHour");
    if(now.value){
        now.value = now.value % 24;
    }
}

//更新分钟输入框
function updateMin(event){
    let now = document.getElementById("inputMin");
    if(now.value){
        now.value = now.value % 60;
    }
}

//提交定时时长
function ConfirmTime(event){
    let hour = document.getElementById("inputHour");
    let min = document.getElementById("inputMin");
    let view = document.getElementById("restTime");
    let page = document.getElementById("gray");

    let val_min;
    let val_hour;
    if(hour.value||min.value){    //非空输入时
      if(hour.value){
        val_hour = Number(hour.value);
      }else{
        val_hour = 0;
      }

      if(min.value){
         val_min = Number(min.value);
      }else{
          val_min = 0;
      }
      restTime = val_hour*60+val_min;
      //更新后端变量
      socketClient.emit("update_var", {restTime: restTime, switch_settime: true}, (data)=>{
        console.log(data);
      });
    }
    

    page.style.transform = "translateX(-100%)";
}

//减少一个小时
function decreaseHour(event){
    let now = document.getElementById("inputHour");
    if(!now.value){
        now.value = 11;
    }else if(now.value==0){
        now.value = 23;
    }
    else{
        now.value--;
    }
}
//增加一个小时
function increaseHour(event){
    let now = document.getElementById("inputHour");
    if(!now.value){
        now.value = 13;
    }else if(now.value==23){
        now.value = 0;
    }else{
        now.value++;
    }
}
//减少一分钟
function decreaseMin(event){
    let now = document.getElementById("inputMin");
    if(!now.value){
        now.value = 59;
    }else if(now.value==0){
        now.value = 59;
    }
    else{
        now.value--;
    }
}
//增加一分钟
function increaseMin(event){
    let now = document.getElementById("inputMin");
    if(!now.value){
        now.value = 1;
    }else if(now.value==59){
        now.value = 0;
    }
    else{
        now.value++;
    }
}

//返回主界面
function returnIndex(event){
    let now = document.getElementById("return");
    let setting = document.getElementById("setting-page");
    let return_sheet = document.getElementById("return-sheet");
    now.style.backgroundColor = "#ccc";

    setTimeout(() => {
        now.style.backgroundColor = "white";
        
        setting.style.transform = "translateX(100%)";
        return_sheet.style.transform = "translateX(100%)";
    }, 100);
    
}

//无人间隔时间的input
function closeInputInterval(event){
    let now = document.getElementById("input-setting-interval");
    let val = now.value;
    
    if(val){
        now.placeholder = val;
        interval_off = Number(val);
        now.value = null;
        //更新后端变量
        socketClient.emit("update_var", {interval_off: interval_off}, (data)=>{
          console.log(data);
        })
    }else{
        now.placeholder = interval_off;
    }
        
}
function closeHighTempre(event){
    let now = document.getElementById("input-high-tempre");
    let val = now.value;
    
    if(val){
        now.placeholder = val;
        max_tempre = Number(val);
        now.value = null;
        //更新后端变量
        socketClient.emit("update_var", {max_tempre: max_tempre}, (data)=>{
          console.log(data);
        })
    }
}
function closeLowTempre(event){
    let now = document.getElementById("input-low-tempre");
    let val = now.value;
    
    if(val){
        now.placeholder = val;
        min_tempre = Number(val);
        now.value = null;
        //更新后端变量
        socketClient.emit("update_var", {min_tempre: min_tempre}, (data)=>{
          console.log(data);
        })
    }
}

//打开/关闭无人自动关机
function closeInterval(event){
    let now = document.getElementById("inputInterval");
    now.style.display = "none";
    autoClose = false;
    //更新后端变量
    socketClient.emit("update_var", {autoClose: autoClose}, (data)=>{
      console.log(data);
    })
}
function openInterval(event){
    let now = document.getElementById("inputInterval");
    now.style.display = "flex";
    autoClose = true;
    //更新后端变量
    socketClient.emit("update_var", {autoClose: autoClose}, (data)=>{
      console.log(data);
    })
}

function openTempre(event){
    let now = document.getElementById("inputTempre");
    let now2 = document.getElementById("inputLowTempre");
    now.style.display = "flex";
    now2.style.display = "flex";
    temp_gate = true;
    //更新后端变量
    socketClient.emit("update_var", {temp_gate: temp_gate}, (data)=>{
      console.log(data);
    })
}
function closeTempre(event){
    let now = document.getElementById("inputTempre");
    let now2 = document.getElementById("inputLowTempre");
    now.style.display = "none";
    now2.style.display = "none";
    temp_gate = false;
    //更新后端变量
    socketClient.emit("update_var", {temp_gate: temp_gate}, (data)=>{
      console.log(data);
    })
}

//打开/关闭开机自动关窗
function OnopenClose(event){
  openClose = true;
  //更新后端变量
  socketClient.emit("update_var", {openClose: openClose}, (data)=>{
    console.log(data);
  })
}
function OffopenClose(event){
  openClose = false;
  //更新后端变量
  socketClient.emit("update_var", {openClose: openClose}, (data)=>{
    console.log(data);
  })
}


function inputWindow(event){
    let now = document.getElementById("windowRange");
    position = now.value;
    //发送控制窗户信息
    socketClient.emit("model_change", devid_model_control, 
    {
      method: "window",
      position: position
    }, (data)=>{
      console.log(data);
    })
    //更新后端变量
    socketClient.emit("update_var", {position: position}, (data)=>{
      console.log(data);
    })
}

function Alert_1(event){
  setting_alert = 1;
  socketClient.emit("update_var", {setting_alert_level: setting_alert}, (data)=>{
    console.log(data);
  })
}
function Alert_2(event){
  setting_alert = 2;
  socketClient.emit("update_var", {setting_alert_level: setting_alert}, (data)=>{
    console.log(data);
  })
}
function Alert_3(event){
  setting_alert = 3;
  socketClient.emit("update_var", {setting_alert_level: setting_alert}, (data)=>{
    console.log(data);
  })
}
function Alert_4(event){
  setting_alert = 4;
  socketClient.emit("update_var", {setting_alert_level: setting_alert}, (data)=>{
    console.log(data);
  })
}
function Alert_5(event){
  setting_alert = 5;
  socketClient.emit("update_var", {setting_alert_level: setting_alert}, (data)=>{
    console.log(data);
  })
}




//定时更新定时页面
setInterval(()=>{
  let now = document.getElementById("restTime");
  if(restTime==0){
    now.innerText = "--:--";
  }else{
    restTime--;
    let min = restTime%60;
    let hour = (restTime-min)/60;
    now.innerText = hour + ":" + min;
  }
}, 1000);

socketClient.on("update_switch", (data)=>{
  // console.log(data);
  Switch = data.Switch;
  let now = document.getElementById("switchicon");

  if(Switch=="open"){
    now.style.backgroundImage = "url(./img/switch/on.png)";
  }else{
    now.style.backgroundImage = "url(./img/switch/off.png)";
  }
})

socketClient.on("update_window", (data)=>{
  let now = document.getElementById("windowRange");
  now.value = data.position;
})

socketClient.on("gas", (data)=>{
  let alert_ = Number(data.alert);
  let page = document.getElementById("mainpage");
  alert_level = alert_;

  socketClient.emit("update_var", {alert_level: alert_level}, (data)=>{
    console.log(data);
  })

  if(alert_==0){
    page.style.backgroundColor = "#efefef";
  }else if(alert_==1){
    page.style.backgroundColor = "#18effa";
  }else if(alert_==2){
    page.style.backgroundColor = "#75f591";
  }else if(alert_==3){
    page.style.backgroundColor = "#edf85d";
  }else if(alert_==4){
    page.style.backgroundColor = "#ef9b48";
  }else if(alert_==5){
    page.style.backgroundColor = "#fa2f2f";
  }
})

socketClient.on("update_wind_model",()=>{
  let icon = document.getElementById("modelid");
  let icon_text = document.getElementById("modeltext");

  icon.style.backgroundImage = "url(./img/model/wind.png)";
  icon_text.innerText = "通风";
})

socketClient.emit("load_var",(data)=>{
  console.log(data);
})